#!/bin/sh
#
####################################################################################################
#
# Author: Rafal Czopa
#
# Description: the purpose of this script is management of user accounts and groups
#
####################################################################################################
#
# Required parameters:
#
#===================================================================================================
#
# creates or deletes user accounts or groups
#-----------------------------------------
# 1) create | delete
# 2) user | group
# 3) <username> | <groupname>
#
# or
#
# unlocks or locks user accounts
#-----------------------------------------
# 1) unlock | lock
# 2) <username>
#
# or
#
# changes password  for given user account
#-----------------------------------------
# 1) chpass
# 2) <username>
# 3) <new_pass>
#
# or
#
# lists all users or lists data of one given user account
#-----------------------------------------
# 1) list
# 2) all | <username>
#
# or
#
# assigns or removes given user account to/from given group
#-----------------------------------------
# 1) assign | remove
# 2) <username>
# 3) <groupname>
#
#
####################################################################################################
#
# Return codes:
#
#===================================================================================================
#
# 0 - ok
# 1 - invalid number of parameters
# 2 - insufficient privileges
# 3 - user or group does not exist
# 4 - user or group already exist
# 5 - unknown error
#
####################################################################################################
#
# Variables:
#
#===================================================================================================
#
mode=""
command=""
action=""
type=""
pass=""
groupname=""
#
####################################################################################################
#
# Functions:
#
#===================================================================================================
#
add_user()
{
	username="$1"

	useradd -m $username

	ret=$?

	if [ $ret -eq 9 ]; then
		echo "User with given name already exist !!!"
		return 4
	elif [ $ret -eq 1 ]; then
		echo "Insufficient privileges !!!"
		return 2
	elif [ $ret -ne 0 ]; then
		echo "Unknown error occured !!!"
		return 5
	else
		return 0
	fi
}

del_user()
{
	username="$1"

	userdel -r $username

	ret=$?

	if [ $ret -eq 6 ]; then
		echo "User with given name does not exist !!!"
		return 3
	elif [ $ret -eq 1 ]; then
		echo "Insufficient privileges !!!"
		return 2
	elif [ $ret -ne 0 ]; then
		echo "Unknown error occured !!!"
		return 5
	else
		return 0
	fi
}

add_group()
{
	groupname="$1"

	echo "wykonuje groupadd $groupname"

	groupadd $groupname

	ret=$?

	if [ $ret -eq 9 ]; then
		echo "Group with given name already exist !!!"
		return 4
	elif [ $ret -eq 1 ]; then
		echo "Insufficient privileges !!!"
		return 2
	elif [ $ret -ne 0 ]; then
		echo "Unknown error occured !!!"
		return 5
	else
		return 0
	fi
}

del_group()
{
	groupname="$1"

	groupdel $groupname

	ret=$?

	if [ $ret -eq 6 ]; then
		echo "User with given name does not exist !!!"
		return 3
	elif [ $ret -eq 1 ]; then
		echo "Insufficient privileges !!!"
		return 2
	elif [ $ret -ne 0 ]; then
		echo "Unknown error occured !!!"
		return 5
	else
		return 0
	fi
}

lock_user()
{
	username="$1"

	usermod -L $username

	ret=$?

	if [ $ret -eq 1 ]; then
		echo "Insufficient privileges or given user does not exist !!!"
		return 2
	elif [ $ret -ne 0 ]; then
		echo "Unknown error occured !!!"
		return 5
	else
		return 0
	fi
}

unlock_user()
{
	username="$1"

	passwd -u $username

	ret=$?

	if [ $ret -eq 1 ]; then
		echo "Insufficient privileges or given user does not exist !!!"
		return 2
	elif [ $ret -ne 0 ]; then
		echo "Unknown error occured !!!"
		return 5
	else
		return 0
	fi
}

change_pass()
{
	username="$1"
	pass="$2"

	passwd $username <<EOF
$pass
$pass
EOF

	ret=$?

	if [ $ret -eq 1 ]; then
		echo "Insufficient privileges or given user does not exist !!!"
		return 2
	elif [ $ret -ne 0 ]; then
		echo "Unknown error occured !!!"
		return 5
	else
		return 0
	fi
}

list_user()
{
	username="$1"

	if [ "$username" != "all" ]; then
		id $username
	else
		cat /etc/passwd
	fi

	ret=$?

	if [ $ret -eq 1 ]; then
		echo "Insufficient privileges or given user does not exist !!!"
		return 2
	elif [ $ret -ne 0 ]; then
		echo "Unknown error occured !!!"
		return 5
	else
		return 0
	fi
}

assign_user_to_group()
{
	username="$1"
	groupname="$2"

	usermod -G $groupname -a $username

	ret=$?

	if [ $ret -eq 6 ]; then
		echo "Group or user with given name does not exist !!!"
		return 3
	elif [ $ret -eq 1 ]; then
		echo "Insufficient privileges !!!"
		return 2
	elif [ $ret -ne 0 ]; then
		echo "Unknown error occured !!!"
		return 5
	else
		return 0
	fi
}

remove_user_from_group()
{
	username="$1"
	groupname="$2"

	gpasswd -d $username $groupname

	ret=$?

	if [ $ret -eq 3 ]; then
		echo "Group/user with given name does not exist or such user does not belong to given group !!!"
		return 3
	elif [ $ret -eq 1 ]; then
		echo "Insufficient privileges !!!"
		return 2
	elif [ $ret -ne 0 ]; then
		echo "Unknown error occured !!!"
		return 5
	else
		return 0
	fi
}
#
####################################################################################################
#
# Script body:
#
#===================================================================================================
#
if [ $# -eq 3 -a "$1" = "create" -o "$1" = "delete" ]; then

	echo 1
	mode=1

elif [ $# -eq 2 -a "$1" = "unlock" -o "$1" = "lock" ]; then

	mode=2

elif [ $# -eq 3 -a "$1" = "chpass" ]; then

	mode=3

elif [ $# -eq 2 -a "$1" = "list" ]; then

	mode=4

elif [ $# -eq 3 -a "$1" = "assign" -o "$1" = "remove" ]; then

	mode=5

else

	echo "Invalid number of parameters"
	exit 1

fi

echo 2

if [ $mode -eq 1 ]; then

	action="$1"
	type="$2"
	name="$3"

	if [ "$action" = "create" ]; then

		if [ "$type" = "user" ]; then
			add_user $name
		elif [ "$type" = "group" ]; then
			add_group $name
		fi

		if [ $? -ne 0 ]; then
			exit $?
		fi

	elif [ "$action" = "delete" ]; then

		if [ "$type" = "user" ]; then
			del_user $name
		elif [ "$type" = "group" ]; then
			del_group $name
		fi

		if [ $? -ne 0 ]; then
			exit $?
		fi

	fi

elif [ $mode -eq 2 ]; then

	action="$1"
	name="$2"

	if [ "$action" = "unlock" ]; then

		unlock_user $name

		if [ $? -ne 0 ]; then
			exit $?
		fi

	elif [ "$action" = "lock" ]; then

		lock_user $name

		if [ $? -ne 0 ]; then
			exit $?
		fi

	fi

elif [ $mode -eq 3 ]; then

	action="$1"
	name="$2"
	pass="$3"

	change_pass $name $pass

	if [ $? -ne 0 ]; then
		exit $?
	fi

elif [ $mode -eq 4 ]; then

	action="$1"
	name="$2"

	list_user $name

	if [ $? -ne 0 ]; then
		exit $?
	fi

elif [ $mode -eq 5 ]; then

	action="$1"
	name="$2"
	groupname="$3"

	if [ "$action" = "assign" ]; then

		assign_user_to_group $name $groupname

		if [ $? -ne 0 ]; then
			exit $?
		fi

	elif [ "$action" = "remove" ]; then

		remove_user_from_group $name $groupname

		if [ $? -ne 0 ]; then
			exit $?
		fi

	fi

fi



exit 0
